package com.ensibs.nuitinfo2013;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.ensibs.nuitinfo2013.model.Collection;
import com.ensibs.nuitinfo2013.model.CollectionPersonnelle;
import com.ensibs.nuitinfo2013.model.Topic;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PersonnalTopicActivity extends Activity{
	
	private ListView listView;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.consultationliste);
		List<Integer> perso = CollectionPersonnelle.getInstance();
		if(perso.size() >0){
		List<Topic> topicPerso = new LinkedList<Topic>();	
		
		for(int index : perso){
			if(Collection.getInstance().getTopicById(index) != null)
				topicPerso.add(Collection.getInstance().getTopicById(index));
		}
		if(topicPerso.size() > 0){
		listView = (ListView) findViewById(R.id.consultation_listview);
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        Iterator<Topic> allTopics = topicPerso.iterator();
        while(allTopics.hasNext()) {        	
        	Topic topic = (Topic) allTopics.next();
        	map = new HashMap<String, String>();
        	map.put("id", Integer.toString(topic.getId()));
            map.put("titre", topic.getTitre());
            if(topic.isResolu()){
            	map.put("description", "résolu");
            }
            else{
            	map.put("description", "en cours"); 
            }
            listItem.add(map);
         }
        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.affichageitem,
                new String[] {"titre", "description"}, new int[] {R.id.consultation_titre, R.id.consultation_description});
  
        listView.setAdapter(mSchedule);
		
        listView.setOnItemClickListener(new OnItemClickListener() {
        	 @Override
         	@SuppressWarnings("unchecked")
          	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        		HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
         		Intent mIntent = new Intent(PersonnalTopicActivity.this, FermerTopicActivity.class); 
         		Bundle mBundle = new Bundle();
         		Topic topic = Collection.getTopicById(Integer.parseInt(map.get("id")));
         		mBundle.putSerializable("topic", topic);
         		mIntent.putExtras(mBundle);
         		startActivity(mIntent);
         	}
		});
		}}
	}
}
